﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace SuperFox.SuperServiceReference
{
    // adding client side features to the user class

    public partial class DbUsers : INotifyPropertyChanged
    {

        // shopping cart

        private Config.WriteStatusValues writeStatus;

        public Config.WriteStatusValues WriteStatus
        {
            get
            {
                return writeStatus;
            }

            set
            {
                writeStatus = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("WriteStatus"));
            }
        }


        private ShoppingCartCollection shoppingCart;

        public ShoppingCartCollection ShoppingCart
        {
            get
            {
                return shoppingCart;
            }

            set
            {
                shoppingCart = value;

                shoppingCart.PropertyChanged += new PropertyChangedEventHandler(shoppingCart_PropertyChanged);
                shoppingCart.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(shoppingCart_CollectionChanged);
                
            }
        }

        void shoppingCart_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if(this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs("ShoppingCart"));
        }

        void shoppingCart_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if(this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs("ShoppingCart"));
        }
        
     
    }

    public partial class Order
    {
        public Order()
        {
            this.PersonalInfo = new PersonalDetails();
            this.ShippingInfo = new ShippingDetails();


            if (Config.IN_DEBUG)
            {
                this.PersonalInfo.Telephone = "123456789";
                this.PersonalInfo.CampaginTitle = "Test Campagin";
                this.PersonalInfo.Company = "Monkey Labs";
                this.PersonalInfo.Email = "adam@yellowrocketperformance.com";
                this.PersonalInfo.Mobile = "0409583025";
                this.PersonalInfo.PersonInfo.FirstName = "Rob";
                this.PersonalInfo.PersonInfo.Surname = "Davis";
                this.PersonalInfo.PersonInfo.Title = "Mr.";
                this.ShippingInfo.AddressInfo.StreetAddress = "20 Furlong Street";
                this.ShippingInfo.AddressInfo.Country = "Australia";
                this.ShippingInfo.AddressInfo.Postcode = "4068";
                this.ShippingInfo.AddressInfo.State = "QLD";
                this.ShippingInfo.AddressInfo.Suburb = "Indooroopilly";
                this.ShippingInfo.BranchName = "Monkey Labs";
                this.ShippingInfo.PersonInfo.FirstName = "Rob";
                this.ShippingInfo.PersonInfo.Surname = "Davis";
                this.ShippingInfo.PersonInfo.Title = "Mr.";
                this.Comments = "This is a test order";

            }

        }
    }

    public partial class ShippingDetails
    {
        public ShippingDetails()
        {
            this.PersonInfo = new Person();

            this.AddressInfo = new Address();

            this.AddressInfo.Country = "Australia";

        }
    }

    public partial class Person
    {
        public override string ToString()
        {
            return Title + " " + FirstName + " " + Surname;
        }
    }

    public partial class Address
    {
        public override string ToString()
        {
            return StreetAddress + ", " + Suburb + ", " + Postcode + ", " + Country;
        }
    }


    public partial class PersonalDetails
    {
        public PersonalDetails()
        {
            this.PersonInfo = new Person();
        }
    }



    public class ShoppingCartCollection : ObservableCollection<DbProducts>, INotifyPropertyChanged
    {

        public decimal GetTotalCost()
        {
            decimal total = 0;

            foreach (DbProducts p in this)
            {
                foreach (DbProductOptionsValues v in p.Options.Values)
                {
                    total += p.prod_price * v.TotalSelected;
                }
            }

            return total;
        }

        public new event PropertyChangedEventHandler PropertyChanged;

        protected override void InsertItem(int index, DbProducts item)
        {
            base.InsertItem(index, item);

            item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
            item.Options.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
            item.Options.Values.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Values_CollectionChanged);

            foreach (DbProductOptionsValues v in item.Options.Values)
            {
                v.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }

        void Values_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs("ShoppinCart"));

            //this.OnPropertyChanged(new PropertyChangedEventArgs("Value"));
        }

        void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs("ShoppingCart"));

            //this.OnPropertyChanged(new PropertyChangedEventArgs("Product"));
        }

        //protected void OnPropertyChanged(string name)
        //{
        //    PropertyChangedEventHandler handler = PropertyChanged;
        //    if (handler != null)
        //    {
        //        handler(this, new PropertyChangedEventArgs(name));
        //    }
        //}


    }
}
